package com.mengma.ec.controller.backend;

import com.mengma.ec.commons.Const;
import com.mengma.ec.commons.ControllerCommons;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.entity.Category;
import com.mengma.ec.entity.User;
import com.mengma.ec.service.portal.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.print.DocFlavor;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: xixi
 * @Date: 2018/9/18 09:12
 * @Description:
 */

@RestController
@RequestMapping(value = "/manage/category/")
public class CatagoryContrller {


    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 1.获取品类子节点(平级)
     * @param categoryId
     * @param session
     * @return
     */
    @PostMapping("get_category.do")
    public ServerResponse<List<Category>> get_catagory(
            @RequestParam(required = true,defaultValue = "0")Integer categoryId, HttpSession session){
        //第一步 从session中获取用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        //1.1判断是否登录
        if (user == null){
            return ServerResponse.createByErrorMessage("请先登录！");
        }
        //1.2拿取用户的唯一角色信息 判断是否有管理员权限
        if(user.getRole() == Const.Role.ROLE_ADMIN){
            //去数据库查询数据 并返回ServerResponse<List<Category>>
            return  iCategoryService.getChildrenParallelCategory(categoryId);
        }
        return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");

    }

    @PostMapping("add_category.do")
    public ServerResponse add_category(
            @RequestParam(required = true,defaultValue = "0")Integer categoryId,String categoryName, HttpSession session){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("请先登录！");
        }
        if(user.getRole().intValue() != Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");
        }
        ServerResponse response = iCategoryService.add_catagory(categoryId,categoryName);
        return response;
    }

    @PostMapping("set_category_name.do")
    public ServerResponse set_category_name(HttpSession session,Integer categoryId,String categoryName){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("请先登录！");
        }
        if(user.getRole().intValue() != Const.Role.ROLE_ADMIN){
            return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");
        }
        ServerResponse response = iCategoryService.set_category_name(categoryId,categoryName);
        return response;
    }

    @PostMapping("get_deep_category.do")
    public ServerResponse<List<Integer>> get_deep_catagory(
            @RequestParam(required = true,defaultValue = "0")Integer categoryId, HttpSession session){
        ServerResponse loginResponse= ControllerCommons.checkLogin(session);
        if (!loginResponse.isSuccess()){
            return ServerResponse.createByErrorMessage("未登录！");
        }
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if(user.getRole().intValue() == Const.Role.ROLE_ADMIN){
            ServerResponse response = iCategoryService.get_deep_catagory(categoryId);
            return response;
        }

        return ServerResponse.createByErrorMessage("你不是管理员，狗带吧！！！");

    }
}
